package com.zcx.test.controller;

import com.zcx.test.common.util.EasyPoiUtils;
import com.zcx.test.common.vo.ApiResult;
import com.zcx.test.mapper.MenuMapper;
import com.zcx.test.pojo.Menu;
import com.zcx.test.pojo.User;
import com.zcx.test.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author zhaochangxin
 * @date 2022/3/4 10:20
 * 菜单控制层
 */
@RestController
@RequestMapping("/menu")
@Api(tags = "菜单模块")
public class MenuController {
    @Autowired
    MenuService menuService;
    private static final String ZERO= "0";
    private static final Integer HUNDRED= 100;
    /**
    　* @author zhaochangxin
    　* @Description:通过菜单id查询到菜单
    　* @Param id:
    　* @return: com.zcx.test.common.vo.ApiResult
    　* @date 2022/3/10 10:23
    　* @throws
    　*/
    @ApiOperation(value = "通过菜单id查询到菜单")
    @RequestMapping("/{id}")
    public ApiResult queryMenuById(@PathVariable("id") String id)
    {
        List<Menu> menus = menuService.queryMenuByParentId(id);
        return ApiResult.success("查询成功",menus);
    }

    /**
    　* @author zhaochangxin
    　* @Description:查询所有菜单
    　* @return: com.zcx.test.common.vo.ApiResult
    　* @date 2022/3/10 10:24
    　* @throws
    　*/
    @ApiOperation(value = "查询所有菜单")
    @RequestMapping("/list")
    public ApiResult queryAllMenu(){
        List<Menu> menus = menuService.queryRootMenu();
        return ApiResult.success("成功",menus);
    }
    /**
    　* @author zhaochangxin
    　* @Description:添加菜单方法
    　* @Param menu:
    　* @return: com.zcx.test.common.vo.ApiResult
    　* @date 2022/3/10 10:28
    　* @throws
    　*/
    @ApiOperation(value = "添加菜单方法")
    @RequestMapping("/add")
    public ApiResult insertMenu(Menu menu){
        menu.setCreatedBy(Long.valueOf(1));
        menu.setCreatedDate(new java.sql.Date(new java.util.Date().getTime()));
        menu.setOwner("zcx");
        menuService.insertMenu(menu);
        return ApiResult.success("添加成功");
    }
    /**
    　* @author zhaochangxin
    　* @Description:删除菜单接口 包括旗下所有的子菜单
    　* @Param id:
    　* @return: com.zcx.test.common.vo.ApiResult
    　* @date 2022/3/10 10:53
    　* @throws
    　*/
    @ApiOperation(value = "删除菜单接口")
    @RequestMapping("/delete/{id}")
    public ApiResult deleteMenuById(@PathVariable("id") String id){
        menuService.deleteMenuById(id);
        return ApiResult.success("删除成功");
    }

    @RequestMapping("/queryUserByMenuId")
    public ApiResult queryUserByMenuId(String id) throws IOException {
        List<User> userList=menuService.queryUserByMenuId(id);
        List<User> userListCopy=new ArrayList<User>(Arrays.asList(new User[userList.size()]));
        Collections.copy(userListCopy,userList);
        EasyPoiUtils.exportExcel(User.class,userList,"src/main/resources/excel/","userList.xls");
        return ApiResult.success("成功",userListCopy);
    }
}
